import { combination, select, reverse } from '../../utils/arrays'
import { set } from '../../utils/stoage'

export const rules = {
  sfc: {name: '胜负彩'},
  r9: {name: '任选9场'}
}

export function bets (bets, rule) {
  if (rule == 'r9') {
    if (bets.length < 9) {
      return 0
    }
    var sum = 0
    combination(bets, 9, function (arr) {
      sum += calc(arr)
    })
    return sum
  } else {
    return bets.length < 14 ? 0 : calc(bets)
  }
}

// 注数计算
function calc (bets) {
  return bets.reduce((val, arr) => {
    return val * arr.length
  }, 1)
}

export function nothing () {
  return [[], [], [], [], [], [], [], [], [], [], [], [], [], []]
}

export const mixin = {
  data: () => ({
    current: {},
    rule: 'sfc',
    bets: nothing()
  }),
  beforeDestroy () {
    set(`winlose.bets`, this.bets)
    set(`winlose.no`, this.current.lotteryNo)
  },
  methods: {
    toggle (prop) { this[prop] = !this[prop] },
    select (i, r) {
      var arr = select(this.bets[i], r).sort(reverse)
      this.$set(this.bets, i, arr)
    },
    on (i, r) {
      var arr = this.bets[i]
      return arr && arr.includes(r)
    }
  },
  computed: {
    selected () {
      return this.bets.filter(b => b && b.length)
    },
    tips () {
      var num = this.rule == 'sfc' ? 14 : 9
      var len = this.selected.length
      if (len == 0) {
        return `请至少选择${num}场比赛`
      }
      var str = `已选${len}场`
      // 已选{{selected.length}}场
      if (len < num) {
        str += `, 还差${num - len}场`
      }
      return str
    },
    sum () {
      return bets(this.selected, this.rule)
    }
  }
}
